﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.MODEL.Statistics;

namespace MINERP.Statistics.ShopBill
{
    public partial class FrmBackOrder : Form
    {
        //参数
        private BLL.Statistics.BackOrderBLL BSB = new MINERP.BLL.Statistics.BackOrderBLL();

        private List<BackOrderEntity.ProductCL> ListProduct = new List<BackOrderEntity.ProductCL>();        //产品集合

        private double AreaPrice = 0;                                                                       //按面积计算的单价

        //属性
        public int ShopId { get; set; }                         //商户ID，应该从前一页面传递过来
        public int BillId { get; set; }                         //账单ID，应该从前一页面传递过来


        public FrmBackOrder()
        {
            InitializeComponent();
        }

        private void FrmBackOrder_Load(object sender, EventArgs e)
        {
            //ShopId = 2; //应从前一页面传递过来
            //BillId = 10;//应从前一页面传递过来
            //绑定产品列表
            BindProduct();
        }

        #region 绑定产品
        /// <summary>
        /// 绑定产品
        /// </summary>
        public void BindProduct()
        {
            ListProduct = BSB.GetProductList(ShopId);
            if (cbProduct.Properties.Items != null || cbProduct.Properties.Items.Count > 0)
                cbProduct.Properties.Items.Clear();
            foreach (BackOrderEntity.ProductCL product in ListProduct)
            {
                cbProduct.Properties.Items.Add(product.ProductName);
            }
        }
        #endregion
        
        //计算面积、金额
        private void btnCompute_Click(object sender, EventArgs e)
        {
            string productName = cbProduct.Text;
            if (productName == "--请选择产品--")
            {
                MessageBox.Show("请选择产品","确认",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
            ComputeAreaAndMoney();
        }

        #region 计算面积、金额
        /// <summary>
        /// 计算面积、金额
        /// </summary>
        private void ComputeAreaAndMoney()
        {
            string productName = cbProduct.Text;
            if (productName == "--请选择产品--")
            {
                MessageBox.Show("请选择产品", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (txtHeight.Text == "" || txtWidth.Text == "")
            {
                MessageBox.Show("请输入高、宽", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            double height = Convert.ToDouble(txtHeight.Text);
            double width = Convert.ToDouble(txtWidth.Text);
            int pageNum = Convert.ToInt16(spinPageNum.Text);
            double mHeight = height * 2.54 / 100;
            double mWidth = width * 2.54 / 100;
            double area = mHeight * mWidth * pageNum;
            double money = area * AreaPrice;
            txtArea.Text = string.Format("{0:0.####}", area);
            txtMoney.Text = string.Format("{0:0.##}", money);
        }
        #endregion

        #region 绑定纸型、纸面、单价
        /// <summary>
        /// 绑定纸型、纸面、单价
        /// </summary>
        private void BindTypeFacePrice(int productId)
        {
            List<string> TypeFacePrice = BSB.GetPaperTypeAndPaperFace(ShopId, productId);
            txtPaperType.Text = TypeFacePrice[0];
            txtPaperFace.Text = TypeFacePrice[1];
            AreaPrice = Convert.ToDouble(TypeFacePrice[2]);
            txtUnitPrice.Text = AreaPrice.ToString();
        }
        #endregion

        //选择了产品，绑定对应的纸型、纸面、价格
        private void cbProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            string productName = cbProduct.Text;
            if (productName == "--请选择产品--")
            {
                MessageBox.Show("请选择产品", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int productId = 0;
            foreach (BackOrderEntity.ProductCL p in ListProduct)
            {
                if (productName == p.ProductName)
                {
                    productId = p.ProductId;
                    break;
                }
            }
            // 绑定纸型、纸面、单价
            BindTypeFacePrice(productId);
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            try
            {
                string productName = cbProduct.Text;
                if (productName == "--请选择产品--")
                {
                    MessageBox.Show("请选择产品", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                } 
                if (txtHeight.Text == "" || txtWidth.Text == "")
                {
                    MessageBox.Show("请输入高、宽", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                int productId = 0;
                foreach (BackOrderEntity.ProductCL p in ListProduct)
                {
                    if (productName == p.ProductName)
                    {
                        productId = p.ProductId;
                        break;
                    }
                }

                //高宽、面积、page数
                double height = Convert.ToDouble(txtHeight.Text);
                double width = Convert.ToDouble(txtWidth.Text);
                int pageNum = Convert.ToInt16(spinPageNum.Text);
                double mHeight = height * 2.54 / 100;
                double mWidth = width * 2.54 / 100;
                double area = mHeight * mWidth * pageNum;
                double money = area * AreaPrice;
                if (height == 0 || width == 0 || pageNum == 0)
                {
                    MessageBox.Show("产品高、宽、Page数都不能为0", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                //客户名称、产品名称
                string ctmName = txtCtmName.Text;
                string pctName = txtProductName.Text;
                if (ctmName == null || ctmName.Length == 0 || pctName == null || pctName.Length == 0)
                {
                    MessageBox.Show("客户名称、产品名称都不能为空", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                //备注
                string remark = txtRemark.Text;
                //开退单
                BSB.BackOrder(BillId, productId, ShopId, height, width, AreaPrice, ctmName, pageNum, pctName, remark);
                MessageBox.Show("开退单成功！","确认",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
